package com.xqboss.chain.echain;

import com.xqboss.chain.config.EChainConfig;
import org.json.JSONObject;

//
//
public class TestContractDeploy {
    //
    public static Account owner = new Account("0xf2de249f6ac81e5c715472f0bd6ec7b430a85155", "28e3f76e0c2bbaa13f33a11afe700462a7093cdeb3a72f52f7591dfa85bb49e2");

    //    static{
////        HttpRequest.setServerCrtPath("D:\\yilian\\e-chain.net.cn_server.crt");
//    }
//
//
    public static void main(String args[]) {
//
        HttpRequest.setServerCrtPath("D:\\Project\\xqboss-share\\xqboss-chain\\src\\main\\resources\\e-chain.net.cn_server.crt");
        try {
            Long l = System.currentTimeMillis();
            String contractAddress = requestDeployContract(l.toString(), "0xf2de249f6ac81e5c715472f0bd6ec7b430a85155");
            System.out.println((System.currentTimeMillis() - l) / 1000);
            System.out.println("部署合约成功，合约地址：" + contractAddress);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //
    public static String requestDeployContract(String reqNo, String owner) throws Exception {
        String payload = Util.formatDeployPayload(reqNo, owner);
        EChainConfig echainConfig = new EChainConfig();
        echainConfig.setBaseUrl("https://qa-api.e-chain.net.cn");
        echainConfig.setMerchantNo("3203001000646087578");
        echainConfig.setAddress("0xf2de249f6ac81e5c715472f0bd6ec7b430a85155");
//        echainConfig.setContractAddress();
        echainConfig.setCallbackUrl("");
        echainConfig.setRsaPrivate("MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCY/i3wvgeqo1gSusHf1AuPYU+nRMNLkZeIPpA6YjvE5iQ26DQXdrQ0WamsvAKz3GEQMwRqmtesEQhLygJj4wJdO4PDhGNqlObsWQx4N1cyrbvGouTbO2hUhnesP3PURMRBEpJd1A/koKmh606i4oKcXKuVBwjoBMdPPCMZ4QchcJ30KFLtiXT9OBpGim7SgFBiKnWxO/4CGd1KkixRj4ID0lyYhRnnCUvFptN822V4g5yr7vSUsH3M7IxVO4SgFzFZjM9pvuo0G2vf51NJ+fK3y9rK24/Vt671sEf59s28OXuyRXErjgfIo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bK+M06Gg5PEOeK1PqwKBgQCn0r+NDE09ImX9PVymwomScpPRWC/SxVgmzx3mGDG0AaRqGjfa1hskqxwx8eRfT3exwvQ9dvYaPyfATyQZ0uEkg7bJ47Jfr7YHkcKMWM1+u8iVxN4mn6Kj3aSfy71iumPzm8J/9BZHX1cTLzXDi1OiP+mQs+UXqwXGfvx/UZHSgw==");
        echainConfig.setPrivateKey("28e3f76e0c2bbaa13f33a11afe700462a7093cdeb3a72f52f7591dfa85bb49e2");

        System.out.println(payload);
        String response = HttpRequest.sendPost("https://qa-api.e-chain.net.cn/chain/contract/deploy", payload, echainConfig);
        JSONObject obj = new JSONObject(response);
        if (!obj.getString("code").equals("EC000000")) {
            throw new Exception("请求合约部署失败：" + obj.getString("message"));
        } else {
            return obj.getJSONObject("data").getString("contractAddress");
        }
    }
}